class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}


// 链表中的位置都为真实位置，不是索引位置

// 单向循环链表
class CircleLinkList {
    size = 0;
    head = null;
    tail = null;

    append(data) {
        const node = new Node(data)
        if (this.size === 0) {
            this.head = node
            node.next = this.head
            this.tail = node
        } else {
            this.tail.next = node
            node.next = this.head
            this.tail = node
        }
        this.size++
    }

    // 根据位置添加元素
    insertIndex(index, data) {
        if (index < 0 || index > this.size) {
            return false
        }
        const node = new Node(data)
        if (this.size === 0) { // 没有长度，直接插入
            this.head = node
            this.tail = node
            this.tail.next = this.head
        }
        if (index === 0 || index === 1) { // 在第一个位置添加元素
            this.tail.next = node
            node.next = this.head
            this.head = node
        }
        if (index > 1 && index <= this.size) {
            let current = this.head
            let previous = null
            for (let i = 1; i < index; i++) {
                previous = current
                current = current.next
            }
            previous.next = node
            node.next = current
        }

        this.size++
    }

    // 删除
    delIndex(index) {
        if (index <= 0 || index > this.size) {
            return false
        }
        if (index === 1) {
            this.tail.next = this.head.next
            this.head = this.head.next
        }
        if (index > 1 && index <= this.size) {
            let current = this.head
            let previous = null
            for (let i = 1; i < index; i++) {
                previous = current
                current = current.next
            }
            if (current === this.tail) {
                previous.next = this.head
                this.tail = previous
            } else {
                previous.next = current.next
            }
        }
        this.size--
        return true
    }

    getSize(){
        return this.size
    }

    // 打印
    print() {
        if (this.size === 0) {
            console.log("这是个空链表");            
            return 
        }
        let node = this.head
        while (node) {
            console.log(node.data);
            if (node === this.tail) {
                break
            } else {
                node = node.next
            }
        }
    }
}

const circleLine = new CircleLinkList()
circleLine.append(1)
circleLine.append(2)
circleLine.append(3)
circleLine.print()
console.log("链表长度为:", circleLine.getSize());

circleLine.insertIndex(2, 0)
circleLine.print()
console.log("链表长度为:", circleLine.getSize());

circleLine.insertIndex(4, 0)
circleLine.print()
console.log("链表长度为:", circleLine.getSize());

circleLine.delIndex(2)
circleLine.print()
